package svc

import (
	"github.com/comfygpt-backend-go/common/database"
	"github.com/comfygpt-backend-go/common/model"
	"github.com/comfygpt-backend-go/service/user-rpc/internal/config"
)

type ServiceContext struct {
	Config config.Config

	UserModel model.UserModel
	MinioCli  *database.MinioClient
}

func NewServiceContext(c config.Config) *ServiceContext {
	conn := database.New(c.Mysql)
	redisCli := database.NewDcRedisClient(c.CacheRedis)

	minioClient := InitDB(c)
	return &ServiceContext{
		Config:    c,
		UserModel: model.NewUserModel(conn, redisCli),
		MinioCli:  minioClient,
	}
}

func InitDB(c config.Config) *database.MinioClient {
	endpoint := c.MinioAuth.Endpoint
	accessKeyID := c.MinioAuth.AccessKey
	secretAccessKey := c.MinioAuth.SecretKey
	sslTrue := c.MinioAuth.UseSSL

	cli, err := database.NewMinio(endpoint, accessKeyID, secretAccessKey, sslTrue)
	if err != nil {
		panic(err)
	}
	return cli
}
